package com.xiye.system.dao;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 西野
 * @version V1.0
 * @title DaoSupport.java
 * @package com.xiye.system.dao
 * @description 数据源访问层封装
 * @date 2018-07-23
 */
@Repository("daoSupport")
@SuppressWarnings("rawtypes")
public class DaoSupport implements DAO {

    @Resource(name = "sqlSessionTemplate")
    private SqlSessionTemplate sqlSessionTemplate;

    /*
     * @Author 西野
     * @Date 2018-07-23 16:40:49
     * @Description 保存对象
     * @Param [str, obj]
     * @Return java.lang.Object
     */
    public Object save(String str, Object obj) {
        return sqlSessionTemplate.insert(str, obj);
    }

    /*
     * @Author 西野
     * @Date 2018-07-23 16:41:02
     * @Description 批量更新
     * @Param [str, objs]
     * @Return java.lang.Object
     */
    public Object batchSave(String str, List objs) {
        return sqlSessionTemplate.insert(str, objs);
    }

    /*
     * @Author 西野
     * @Date 2018-07-23 16:41:08
     * @Description 修改对象
     * @Param [str, obj]
     * @Return java.lang.Object
     */
    public Object update(String str, Object obj) {
        return sqlSessionTemplate.update(str, obj);
    }

    /*
     * @Author 西野
     * @Date 2018-07-23 16:41:17
     * @Description 批量更新
     * @Param [str, objs]
     * @Return void
     */
    public void batchUpdate(String str, List objs) {
        SqlSessionFactory sqlSessionFactory = sqlSessionTemplate.getSqlSessionFactory();
        // 批量执行器
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            if (objs != null) {
                objs.forEach(o -> sqlSession.update(str, o));
                sqlSession.flushStatements();
                sqlSession.commit();
                sqlSession.clearCache();
            }
        } finally {
            sqlSession.close();
        }
    }

    /*
     * @Author 西野
     * @Date 2018-07-23 16:41:24
     * @Description 批量更新
     * @Param [str, objs]
     * @Return java.lang.Object
     */
    public Object batchDelete(String str, List objs) {
        return sqlSessionTemplate.delete(str, objs);
    }

    /*
     * @Author 西野
     * @Date 2018-07-23 16:41:29
     * @Description 删除对象
     * @Param [str, obj]
     * @Return java.lang.Object
     */
    public Object delete(String str, Object obj) {
        return sqlSessionTemplate.delete(str, obj);
    }

    /*
     * @Author 西野
     * @Date 2018-07-23 16:41:36
     * @Description 查找对象
     * @Param [str, obj]
     * @Return java.lang.Object
     */
    public Object findForObject(String str, Object obj) {
        return sqlSessionTemplate.selectOne(str, obj);
    }

    /*
     * @Author 西野
     * @Date 2018-07-23 16:41:42
     * @Description 查找对象
     * @Param [str, obj]
     * @Return java.lang.Object
     */
    public Object findForList(String str, Object obj) {
        return sqlSessionTemplate.selectList(str, obj);
    }

    /**
     * @Author 西野
     * @Date 2018-07-23 16:41:50
     * @Description 查找对象
     * @Param [str, obj, key, value]
     * @Return java.lang.Object
     */
    public Object findForMap(String str, Object obj, String key, String value) {
        return sqlSessionTemplate.selectMap(str, obj, key);
    }

}


